/*
 * Copyright 2015-2016 TakWolf
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dxyxx.framework;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import cn.pedant.SweetAlert.SweetAlertDialog;
import cn.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;

import com.dxyxx.R;

public class CrashLogActivity extends Activity {

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.CHINA);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_crash_log);

		Throwable e = (Throwable) getIntent().getSerializableExtra("e");

		StringBuilder sb = new StringBuilder();
		sb.append("生产厂商：\n");
		sb.append(Build.MANUFACTURER).append("\n\n");
		sb.append("手机型号：\n");
		sb.append(Build.MODEL).append("\n\n");
		sb.append("系统版本：\n");
		sb.append(Build.VERSION.RELEASE).append("\n\n");
		sb.append("异常时间：\n");
		sb.append(dateFormat.format(new Date())).append("\n\n");
		sb.append("异常类型：\n");
		sb.append(e.getClass().getName()).append("\n\n");
		sb.append("异常信息：\n");
		sb.append(e.getMessage()).append("\n\n");
		sb.append("异常堆栈：\n");
		Writer writer = new StringWriter();
		PrintWriter printWriter = new PrintWriter(writer);
		e.printStackTrace(printWriter);
		Throwable cause = e.getCause();
		while (cause != null) {
			cause.printStackTrace(printWriter);
			cause = cause.getCause();
		}
		printWriter.close();
		String result = writer.toString();
		sb.append(result);

		TextView tvInfo = (TextView) findViewById(R.id.crash_log_tv_info);
		tvInfo.setText(sb.toString());
		SweetAlertDialog sw = new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE);
		sw.setTitleText("Oops...").setContentText(" 错误在所难免，我们追求更好\n--大象与猩猩--").show();
		sw.setConfirmText("谢谢谅解");
		sw.setConfirmClickListener(new OnSweetClickListener() {

			@Override
			public void onClick(SweetAlertDialog sweetAlertDialog) {
				// 发送报告
				finish();
			}
		});
		sw.setCancelable(false);//只能点击ok取消
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub

	}
}
